{layout name="layout"}
<link  href="__PLU__/bootstrap-validator/bootstrapValidator.min.css" rel="stylesheet">
<script src="__PLU__/bootstrap-validator/bootstrapValidator.min.js"></script>
<script src="__PLU__/bootbox/bootbox.min.js"></script>
<div class="panel">
    <div class="panel-body">
        <div class="row">
            <div class="col-sm-offset-3 col-sm-6">
                <form class="form-horizontal" id="modifypwd-form" action="{:url('pam/member/modify_pwd')}">
                    <div class="form-group">
                        <div class="col-sm-2 text-center">
                            <label for="pwd-old" class="control-label">旧密码</label>
                        </div>
                        <div class="col-sm-8">
                            <input type="password" id="pwd-old" name="password[old]" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-2 text-center">
                            <label for="pwd-new" class="control-label">新密码</label>
                        </div>
                        <div class="col-sm-8">
                            <input type="password" id="pwd-new" name="password[new]" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-2 text-center">
                            <label for="pwd-chk" class="control-label">确认密码</label>
                        </div>
                        <div class="col-sm-8">
                            <input type="password" id="pwd-chk" name="password[chk]" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-2 text-center">
                            <label class="control-label"></label>
                        </div>
                        <div class="col-sm-8 text-center">
                            <button class="btn btn-lg btn-info" form="modifypwd-form" data-loading-text="Loading..." type="button">
                                保存
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        
    </div>
</div>
<script type="text/javascript">
$(document).ready(function(){
    $('#modifypwd-form').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon',
            invalid: 'glyphicon',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            "password[old]": {
                validators: {
                    notEmpty: {
                        message: '请输入旧密码'
                    },
                    regexp: {
                        regexp: /[0-9a-zA-Z]{6,16}/,
                        message: '密码为字母和数字，6-16位'
                    }
                }
            },
            "password[new]": {
                validators: {
                    notEmpty: {
                        message: '请输入新密码'
                    },
                    regexp: {
                        regexp: /[0-9a-zA-Z]{6,16}/,
                        message: '密码为字母和数字，6-16位'
                    }
                }
            },
            "password[chk]": {
                validators: {
                    notEmpty: {
                        message: '请输入确认密码'
                    },
                    regexp: {
                        regexp: /[0-9a-zA-Z]{6,16}/,
                        message: '密码为字母和数字，6-16位'
                    }
                }
            }
        }
    });
});
$('button[form="modifypwd-form"]').on('click', function () {
    var btn = $(this).button('loading');
    $('#modifypwd-form').data('bootstrapValidator').validate();  
    if(!$('#modifypwd-form').data('bootstrapValidator').isValid()){  
        btn.button('reset');
        return false;
    }
    $.ajax({
        url:"{:url('admin/center/save_pwd')}",
        type:"post",
        data:$('#modifypwd-form').serialize(),
        dataType:"json",
        success:function(result){
            if(result.status == 'success'){
                $.niftyNoty({
                    type: 'success',
                    icon : 'pli-exclamation icon-2x',
                    message : result.msg,
                    container : 'floating',
                    timer : 1500,
                });
                btn.button('reset');
                setTimeout(function(){
                    window.location.reload();
                },1500);

            }else{
                $.niftyNoty({
                    type: 'warning',
                    icon : 'pli-exclamation icon-2x',
                    message : result.msg,
                    container : 'floating',
                    timer : 1500,
                });
                btn.button('reset');
            }
        },
        error:function(){
            $.niftyNoty({
                type: 'danger',
                icon : 'pli-exclamation icon-2x',
                message : '网络连接失败',
                container : 'floating',
                timer : 1500,
            });
            btn.button('reset');
        }
    });
});
</script>